<template>
    <el-dialog width="50%"
               :close-on-click-modal="false"
               @open="handleOpen"
               :modelValue="dialogVisible"
               @update:modelValue="(e)=>{emit('update:dialogVisible',e)}"
               :title="type==='add'?t('page.user.addUser'):t('page.user.editUser')">
        <el-form :model="formParams" class="form-box">
            <el-form-item :label="t('page.user.userName')" :label-width="formLabelWidth">
                <el-input v-model="formParams.userName" autocomplete="off" />
            </el-form-item>
            <el-form-item :label="t('page.user.name')" :label-width="formLabelWidth">
                <el-input v-model="formParams.name" autocomplete="off" />
            </el-form-item>
            <el-form-item :label="t('page.user.email')" :label-width="formLabelWidth">
                <el-input v-model="formParams.email" autocomplete="off" />
            </el-form-item>
            <el-form-item :label="t('page.user.phoneNumber')" :label-width="formLabelWidth">
                <el-input v-model="formParams.phoneNumber" autocomplete="off" />
            </el-form-item>
            <el-form-item :label="t('page.user.roleNames')" :label-width="formLabelWidth">
                <el-select
                    v-model="formParams.roleNames"
                    multiple
                    collapse-tags
                    placeholder="Select"
                >
                    <el-option
                        v-for="item in roles"
                        :key="item.name"
                        :label="item.name"
                        :value="item.name"
                    />
                </el-select>
            </el-form-item>
            <el-form-item :label="t('page.user.password')" :label-width="formLabelWidth">
                <el-input v-model="formParams.password" autocomplete="off" />
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="emit('update:dialogVisible',false)">{{ t('global.dialog.cancel') }}</el-button>
                <el-button type="primary" @click="sureClick">
                    {{ t('global.dialog.confirm') }}
                </el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script setup>
import { reactive, ref, watch } from 'vue'
import {usersCreate,usersRole,usersUpdate} from "@/api/user"
import { ElMessage, ElMessageBox } from 'element-plus'
import {omit,pick} from "lodash-es"
import { useI18n } from '@/hooks/usel18n';
defineOptions({
  name: "UserDialog",
});
const props = defineProps({
  dialogVisible: {
    type:Boolean,
    default:false
  },
  roles:{
    type:Array,
    default:function() {
      return []
    }
  },
  userRoles:{
    type:Array,
    default:function() {
      return []
    }
  },
  type:{
    type:String,
    default:'add'
  },
  editObject:{
    type:Object,
    default:function() {
      return null
    }
  }
})
const { t } = useI18n();
const emit = defineEmits(['update:dialogVisible','hanldeRequest']);
const formLabelWidth = '120px'
const formParams = ref({
  userName:'',
  name:'',
  surname:'',
  email:'',
  phoneNumber:'',
  isActive:true,
  lockoutEnabled:true,
  roleNames:[],
  password:'',
})

function handleOpen() {
  if(props.type === 'add') {
    formParams.value = {
      userName:'',
      name:'',
      surname:'',
      email:'',
      phoneNumber:'',
      isActive:true,
      lockoutEnabled:true,
      roleNames:[],
      password:'',
    }
  }else if(props.type === 'edit') {
    formParams.value = {...pick(props.editObject,['userName','name','surname','email','phoneNumber','isActive','lockoutEnabled','roleNames']),password:''} //Object.assign(formParams.value,props.editObject)
  }

}
async function sureClick() {
  if(props.type === 'add') {
    await usersCreate(formParams.value)
    ElMessage({
      type: 'success',
      message: '新增成功',
    })
  }else if(props.type === 'edit') {
    await usersUpdate({
      userId:props.editObject.id,
      userInfo:formParams.value
    })
    ElMessage({
      type: 'success',
      message: '修改成功',
    })
  }

  emit('update:dialogVisible',false)
  emit('hanldeRequest')
}


</script>

<style lang="scss" scoped>
.form-box {
  display: flex;
  flex-wrap: wrap;
  .el-form-item {
    width: 50%;
  }
}
</style>